<?


if ($_POST) {
    if (!$_POST["clau"]) {
        $_POST["clau"]="";
    }

    $clau = $_POST["clau"];
}

$ip = $_SERVER['REMOTE_ADDR'];

// Define messages
$msg_noClau = $labels["LBL_PSSWD_REQUIRED"];
$msg_clau_no_ok = $labels["LBL_WRONG_PSSWD"];
$msg_alta_wait = "<img src='/files/loading.gif'>";
$msg_alta_ok = $labels["LBL_REGISTER_OK"];

error_reporting(0);

$errors="";
if ($_POST) {
    
    if (($clau=="")) $errors[] = $msg_noClau;

    if (($clau<>"")) {

        // Connect to DB
        $con = mysql_connect ($db_host, $db_user, $db_pswd) or die ($labels["LBL_DB_CONN_ERR"] . mysql_error());
        $db = mysql_select_db ($db_name) or die ($labels["LBL_DB_ERROR"]);
        //include ($app_inc_path."db_connect.php");
        
        // Test if password is OK
        $sql="SELECT * FROM `x_alta` WHERE clau='$clau'";
        $fila = mysql_query($sql);
        $registre= mysql_fetch_row($fila);

        // Get data
        $clauOK=$registre[1];

        // Wrong password or password not provided
        if ($clauOK=="") {
            $errors[]= $msg_clau_no_ok;
        }

        // Password found. Registration confirmed. Insert IP address on DB.
        if ($clauOK<>"") {
            $alta = "UPDATE `x_alta` SET ip='$ip' WHERE clau='$clau'";
            mysql_query($alta);
            $actiu="";

            while ($actiu=="") {
                $sql="SELECT * FROM `x_actius` WHERE clau='$clau'";
                $fila = mysql_query($sql);
                $actiu= mysql_num_rows($fila);
                sleep(1);
            }

            $errors[]= $msg_alta_ok;
        }

        // Disconnect
        mysql_close ($con);
        //include ($app_inc_path."db_disconnect.php");

    }
}

$skin = new Skinner();

if ($errors) {
    $skin->setSkin($skins_path."errors");
    $skin->registerVariable("errors", $errors[0]);
} else {
    $skin->setSkin($skins_path."form");
    $item_value = ($_POST["clau"] ? stripslashes($_POST["clau"]) : stripslashes(""));
    $skin->registerVariable("prompt", $labels["LBL_PSSWD_PROMPT"]);
    $skin->registerVariable("item_value", $item_value);
}

$output = $skin->doSubstitution_FAST();

?>
